package com.sf.asp.controller;


import com.sf.asp.entity.Role;
import com.sf.asp.entity.dto.DeleteDTO;
import com.sf.asp.entity.dto.RoleQueryDTO;
import com.sf.asp.entity.dto.RoleSaveDTO;
import com.sf.asp.entity.vo.PageInfo;
import com.sf.asp.entity.vo.ResponseInfo;
import com.sf.asp.entity.vo.RoleVO;
import com.sf.asp.service.RoleService;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jins
 * @since 2021-11-24
 */
@Controller
public class RoleController {


    private final RoleService roleService;

    public RoleController (RoleService roleService){
        this.roleService = roleService;
    }

    /*------------------------------角色的页面------------------------*/

    @GetMapping("/role")
    public String rolePage(){
        return "manage/role/list";
    }

    @GetMapping("/role/save")
    public String roleSavePage(){
        return "manage/role/save";
    }

    @GetMapping("/role/edit/{roleId}")
    public String roleEditPage(@PathVariable Integer roleId, ModelMap map){
        RoleVO roleVO = roleService.getRoleById(roleId);
        map.addAttribute("role",roleVO);
        return "manage/role/edit";
    }




    /*------------------------------角色的接口------------------------*/

    @GetMapping("/roles")
    @ResponseBody
    public PageInfo roles(RoleQueryDTO roleParam){
        return roleService.listRole(roleParam);
    }

    @PostMapping("/role/save")
    @ResponseBody
    public ResponseInfo saveRoles(@RequestBody RoleSaveDTO role){

        roleService.saveRole(role);
        return ResponseInfo.ok();
    }

    @PostMapping("/role/delete")
    @ResponseBody
    public ResponseInfo deleteRole(@RequestBody DeleteDTO delete){
        roleService.deleteRole(delete);
        return ResponseInfo.ok();
    }

    @PostMapping("/role/edit")
    @ResponseBody
    public ResponseInfo editRole(@RequestBody RoleVO roleVO){
        roleService.editRole(roleVO);
        return ResponseInfo.ok();
    }


    /**
     * 为用户管理页面提供所有的角色
     * @return
     */
    @GetMapping("/roles/for-user")
    @ResponseBody
    public ResponseInfo listRoleForUser(){
        return ResponseInfo.ok(roleService.listRoleForUser());
    }



}
